"""Added final_unit_price to CartProduct and OrderLine

Revision ID: 2b97252add64
Revises: 753a76783127
Create Date: 2024-02-25 11:58:52.715790

"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "2b97252add64"
down_revision = "753a76783127"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("bonus_history")
    op.execute("DROP TYPE IF EXISTS operationtype")
    op.execute("CREATE TYPE operationtype AS ENUM ('ACCRUAL', 'DEDUCTION')")
    op.create_table(
        "bonus_operation",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=True),
        sa.Column("operation_type", sa.Enum("ACCRUAL", "DEDUCTION", name="operationtype"), nullable=False),
        sa.Column("amount", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=True),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.text("now()"), nullable=True),
        sa.ForeignKeyConstraint(["user_id"], ["user.id"], name=op.f("fk_bonus_operation_user_id_user")),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_bonus_operation")),
    )

    op.add_column("cart_product", sa.Column("final_unit_price", sa.Float(), nullable=False))
    op.add_column("order", sa.Column("bonuses_amount", sa.Integer(), nullable=False, server_default="0"))
    op.add_column("order_line", sa.Column("final_unit_price", sa.Float(), nullable=True, server_default="0"))
    op.alter_column("order_line", "final_unit_price", nullable=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column("order_line", "final_unit_price")
    op.drop_column("order", "bonuses_amount")
    op.drop_column("cart_product", "final_unit_price")
    op.drop_table("bonus_operation")
    op.create_table(
        "bonus_history",
        sa.Column("id", sa.INTEGER(), autoincrement=True, nullable=False),
        sa.Column("user_id", sa.INTEGER(), autoincrement=False, nullable=True),
        sa.Column(
            "operation_type",
            postgresql.ENUM("ACCRUAL", "DEDUCTION", name="operationtype"),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column("amount", sa.INTEGER(), autoincrement=False, nullable=False),
        sa.Column(
            "created_at",
            postgresql.TIMESTAMP(),
            server_default=sa.text("now()"),
            autoincrement=False,
            nullable=True,
        ),
        sa.Column(
            "updated_at",
            postgresql.TIMESTAMP(),
            server_default=sa.text("now()"),
            autoincrement=False,
            nullable=True,
        ),
        sa.ForeignKeyConstraint(["user_id"], ["user.id"], name="fk_bonus_history_user_id_user"),
        sa.PrimaryKeyConstraint("id", name="pk_bonus_history"),
    )
    # ### end Alembic commands ###
